home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / UniqueKey_sybase / MasterDetail.m < prev    next >
Text File  |  1994-06-14  |  8KB  |  185 lines

  1. #import "UniqueKey.h"
  2. #import "MasterDetail.h"
  3.  
  4.  
  5. static BOOL _Debug = YES;
  6.  
  7. @implementation MasterDetail
  8.  
  9. /******************************************************************************
  10. * Create an instance of the UniqueKey object to grab blocks of <count> keys for
  11. * our use during insert.  The count is set low so you can see the SQL reserve
  12. * blocks of keys.  Up the count to something larger for it to be of use.
  13. *
  14. * The login information (connection dictionary) has been blanked out, since
  15. * you may elect to have the tables in some database other than PEOPLE, so we
  16. * elect to run a login panel.  Once the login is complete we can get the
  17. * connection dictionary from the adaptor and use that information to allow
  18. * UniqueKey to login and create its separate connection for key reservation.
  19. *
  20. * Many of the method calls in EOF return (id)<some protocol>, so you will notice
  21. * several casts to (id) to avoid warnings for nested method calls supported by
  22. * the returned object, but not by the protocol.  Alternatively, you can cast to
  23. * the exact class you know returned, such as (EODatabaseDataSource*) or
  24. * (EODetailDatabaseSource*).
  25. ******************************************************************************/
  26. - appDidInit:sender
  27. {
  28.     EOAdaptorChannel *eoAdaptorChannel = [[(id)[employeeController dataSource] databaseChannel] adaptorChannel];
  29.     EOAdaptor        *eoAdaptor        = [[eoAdaptorChannel adaptorContext] adaptor];
  30.     
  31.     if(_Debug) [eoAdaptorChannel setDelegate:self];
  32.  
  33.     employeeEntity       = [[(id)[employeeController dataSource] entity] retain];
  34.     equipmentOwnerEntity = [[(id)[equipmentOwnerController dataSource] entity] retain];
  35.  
  36.     if(![eoAdaptor runLoginPanelAndValidateConnectionDictionary]) [NXApp terminate:self];
  37.     [UniqueKey setConnectionDictionary:[eoAdaptor connectionDictionary]];
  38.     
  39.     employeeUniqueKey = [[[UniqueKey alloc] initWithEntity:employeeEntity count:5] retain];
  40.     if(!employeeUniqueKey) [NXApp terminate:self];
  41.  
  42.     [self setFetchOrderFor:employeeController with:@"LastName" order:EOAscendingOrder];
  43.     [self setFetchOrderFor:equipmentOwnerController with:@"Description" order:EOAscendingOrder];
  44.  
  45.     [employeeController fetch:self];
  46.     return self;
  47. }
  48.  
  49.  
  50. /******************************************************************************
  51. * Set a controller's data source to fetch sorted by a given attribute name
  52. * and order.
  53. ******************************************************************************/
  54. - setFetchOrderFor:(EOController*)controller with:(NSString*)attributeName order:(EOOrdering)order
  55. {
  56.     id        dataSource = [controller dataSource];
  57.     id        attribute  = [[dataSource entity] attributeNamed:attributeName];
  58.     NSArray    *orderArray;
  59.     
  60.     orderArray = [NSArray arrayWithObject:
  61.         [EOAttributeOrdering attributeOrderingWithAttribute:attribute ordering:order]];
  62.     [dataSource setFetchOrder:orderArray];
  63.     return self;
  64. }
  65.  
  66.  
  67. /******************************************************************************
  68. * Generate unique keys for the new object.  Use the instance of UniqueKey
  69. * to dole out a key from its internal buffer.  The UniqueKey object has its
  70. * own channel to the DB which it uses to allocate blocks of keys.
  71. ******************************************************************************/
  72. - (BOOL)controller:controller willInsertObject:object;
  73. {
  74.     if(controller==employeeController)
  75.     {
  76.     NSNumber       *uniqueKey  = [NSNumber numberWithInt:[employeeUniqueKey nextKey]];
  77.     NSArray        *emptyArray = [[[NSArray alloc] init] autorelease];
  78.  
  79.     [object setObject:uniqueKey      forKey:@"EmpId"];
  80.     [object setObject:@"<LastName>"  forKey:@"LastName"];
  81.     [object setObject:@"<FirstName>" forKey:@"FirstName"];
  82.     [object setObject:@"<Phone>"     forKey:@"Phone"];
  83.     [object setObject:emptyArray     forKey:@"toEmpEquipment"];
  84.     return YES;
  85.     }
  86.     return NO;
  87. }
  88.  
  89.  
  90. /******************************************************************************
  91. * When an employee is deleted from the database, we need to remove that person's
  92. * ownership of their equipment.  We use the toEmpEquipment relationship to get
  93. * an NSArray of equipment objects and null the EmpId for each.
  94. ******************************************************************************/
  95. - (BOOL)controller:controller willDeleteObject:object
  96. {
  97.     if(controller==employeeController) {
  98.         NSArray     *eeArray      = [object objectForKey:@"toEmpEquipment"];
  99.     NSEnumerator    *eeEnumerator = [eeArray objectEnumerator];
  100.     EOGenericRecord    *equipment;
  101.     
  102.     while(equipment = [eeEnumerator nextObject]) {
  103.         [equipment setObject:[EONull null] forKey:@"EmpId"];
  104.         [(id)[employeeController dataSource] updateObject:equipment];
  105.     }
  106.     }
  107.     return YES;
  108. }
  109.  
  110.  
  111. /******************************************************************************
  112. * Bring up the assign equipment panel and start a modal session.  Construct 
  113. * an 'otherEquipment' qualifier to select equipment not currently assigned to
  114. * the selected employee.
  115. ******************************************************************************/
  116. - assignEquipmentToEmployee:sender
  117. {
  118.     EOGenericRecord    *employee = [(id)[equipmentForEmployeeController dataSource] masterObject];
  119.     NSNumber        *empId    = [employee objectForKey:@"EmpId"];
  120.     EOQualifier        *otherEquipment;
  121.     NSString        *qualifierString;
  122.     
  123.     qualifierString = [NSString stringWithFormat:@"(EmpId != %@ or EmpId = NULL)",empId];
  124.     
  125.     otherEquipment = [[[EOQualifier alloc]
  126.          initWithEntity:equipmentOwnerEntity qualifierFormat:qualifierString] autorelease];
  127.     
  128.     [(id)[equipmentOwnerController dataSource] setQualifier:otherEquipment];
  129.     [equipmentOwnerController clearSelection];
  130.     [equipmentOwnerController fetch];
  131.     [[assignEquipmentPanel center] makeKeyAndOrderFront:nil];
  132.     [NXApp runModalFor:assignEquipmentPanel];
  133.     [assignEquipmentPanel orderOut:self];
  134.     [employeeController fetch];
  135.     return self;
  136. }
  137.  
  138. - assignEquipmentToEmployeeOK:sender
  139. {
  140.     EOGenericRecord    *employee       = [(id)[equipmentForEmployeeController dataSource] masterObject];
  141.     NSNumber        *empId          = [employee objectForKey:@"EmpId"];
  142.     NSArray        *eoArray    = [equipmentOwnerController selectedObjects];
  143.     NSEnumerator    *eoEnumerator    = [eoArray objectEnumerator];
  144.     EOGenericRecord    *equipmentOwner;
  145.  
  146.     while(equipmentOwner = [eoEnumerator nextObject]) {
  147.     [equipmentOwner setObject:empId forKey:@"EmpId"];
  148.     [(id)[equipmentOwnerController dataSource] updateObject:equipmentOwner];
  149.     }
  150.     [NXApp stopModal];
  151.     return self;
  152. }
  153.  
  154.  
  155. /******************************************************************************
  156. * Release the equipment shown in the detail view.  Null out the equipment owner's
  157. * EmpId for each.
  158. ******************************************************************************/
  159. - releaseEquipmentForEmployee:sender
  160. {
  161.     NSArray        *efeArray    = [equipmentForEmployeeController selectedObjects];
  162.     NSEnumerator    *efeEnumerator    = [efeArray objectEnumerator];
  163.     EOGenericRecord    *equipment;
  164.  
  165.     while(equipment = [efeEnumerator nextObject]) {
  166.     [equipment setObject:[EONull null] forKey:@"EmpId"];
  167.     [(id)[equipmentForEmployeeController dataSource] updateObject:equipment];
  168.     }
  169.     [employeeController fetch];
  170.     return self;
  171. }
  172.  
  173.  
  174. /******************************************************************************
  175. * Echo SQL when debug is enabled.
  176. ******************************************************************************/
  177. - (EODelegateResponse)adaptorChannel:channel willEvaluateExpression:(NSMutableString *)expression
  178. {
  179.     NSLog(expression);
  180.     return EODelegateApproves;
  181. }
  182.  
  183.  
  184. @end
  185.